package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品分类
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //给Dish表赋值
        this.save(dishDto);
        //获取Dish表的id给Dish_flavor表dish_id赋值
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));
        //给Dish_flavor表赋值
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 分页查询
     */
    @Override
    public Page<DishDto> dishWishDishDto(Integer page, Integer pageSize, String name) {

        Page<Dish> pageInfo1 = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> queryWrapper = Wrappers.lambdaQuery(Dish.class).like(StringUtils.isNotBlank(name), Dish::getName, name)
                .orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        this.page(pageInfo1, queryWrapper);

        Page<DishDto> pageInfo2 = new Page<>(page, pageSize);
        BeanUtils.copyProperties(pageInfo1, pageInfo2, "records");

        List<Dish> dish = pageInfo1.getRecords();
        List<DishDto> dishDtos = dish.stream().map(dis -> {

            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dis, dishDto);
            Category category = categoryService.getById(dis.getCategoryId());
            dishDto.setCategoryName(category.getName());
            return dishDto;
        }).collect(Collectors.toList());

        pageInfo2.setRecords(dishDtos);

        return pageInfo2;
    }

    /**
     * 数据回显
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {

        //查询菜品基本信息，从dish表查询
        Dish dish = this.getById(id);
        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = Wrappers.lambdaQuery(DishFlavor.class)
                .eq(DishFlavor::getDishId, id);
        List<DishFlavor> dishFlavors = dishFlavorService.list(queryWrapper);
        //将菜品与口味信息封装到Dto中
        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(dishFlavors);

        return dishDto;
    }

    /**
     * 修改菜品
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);

        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = Wrappers.lambdaQuery(DishFlavor.class)
                .eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(f->f.setDishId(dishDto.getId()));
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 批量删除
     */
    @Override
    @Transactional
    public void deleteWithFlavor(Long[] ids) {
        LambdaQueryWrapper<Dish> queryWrapper = Wrappers.lambdaQuery(Dish.class)
                .eq(Dish::getStatus, 1)
                .in(Dish::getId, ids);
        int count = this.count(queryWrapper);
        if (count > 0) {
            throw new CustomerException("处于售卖中的菜品,不能删除");
        }

        this.removeByIds(Arrays.asList(ids));

        LambdaQueryWrapper<DishFlavor> wrapper = Wrappers.lambdaQuery(DishFlavor.class)
                .in(DishFlavor::getDishId, ids);

        dishFlavorService.remove(wrapper);
    }
}
